﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace Walson.IO
{
    public class IniFile
    {
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        //         [DllImport("kernel32")]
        //         private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);


        private string _IniFile = string.Empty;

        private Dictionary<string, Dictionary<string, string>> _FileMap = new Dictionary<string, Dictionary<string, string>>();
        //private Dictionary<string, Dictionary<string, string>> _Memos = new Dictionary<string, Dictionary<string, string>>();


        public Dictionary<string, string> this[string section]
        {
            get
            {
                return _FileMap[section];
            }
        }


        /// <summary>
        /// INI 文件读写
        /// </summary>
        /// <param name="fileName">文件名</param>
        public IniFile(string fileName)
        {
            _IniFile = fileName;
            if (File.Exists(fileName))
            {
                string[] lines = File.ReadAllLines(fileName);
                StringBuilder sb = new StringBuilder();
                // bool memoFound = false;
                string section = string.Empty;
                string[] item;

                foreach (string line in lines)
                {
                    if (line.Trim() == string.Empty)
                    {
                        continue;
                    }
                    else if (line.StartsWith(";"))
                    {
                        continue;
                        //                         memoFound = true;
                        //                         sb.Append(line);
                    }
                    else if (line.StartsWith("["))
                    {
                        section = line.Substring(1, line.Length - 2);
                        _FileMap.Add(section, new Dictionary<string, string>());
                        //_Memos.Add(section, new Dictionary<string, string>());
                        //                             if (memoFound)
                        //                             {
                        //                                 memoFound = false;
                        //                                 _Memos[section].Add("section", sb.ToString());
                        //                                 sb.Remove(0, sb.Length);
                        //                             }
                    }
                    else if (line.Contains("="))
                    {
                        item = line.Split('=');
                        _FileMap[section].Add(item[0].Trim(), item[1].Trim());
                        //                             if (memoFound)
                        //                             {
                        //                                 memoFound = false;
                        //                                 _Memos[section].Add(item[0].Trim(), sb.ToString());
                        //                                 sb.Remove(0, sb.Length);
                        //                             }
                    }

                }
            }
        }

        /// <summary>
        /// 写入配置参数
        /// </summary>
        /// <param name="section">要写入的 Section 名称，若不存在则创建</param>
        /// <param name="key">要写入的 key 名称，若不存在则创建</param>
        /// <param name="value">要写入的值</param>
        public void Write(string section, string key, string value)
        {
            if (_FileMap.ContainsKey(section) == false)
            {
                _FileMap.Add(section, new Dictionary<string, string>());
            }

            if (_FileMap[section].ContainsKey(key))
            {
                _FileMap[section][key] = value;
            }
            else
            {
                _FileMap[section].Add(key, value);
            }

            WritePrivateProfileString(section, key, value, _IniFile);
        }

        //         /// <summary>
        //         /// 读指定位置的配置
        //         /// </summary>
        //         /// <param name="section">要读取的 Section</param>
        //         /// <param name="key">要读取的 Key</param>
        //         /// <returns>值，若指定的位置不存在，则返回空</returns>
        //         public string Read(string section,string key)
        //         {
        //             if(_FileMap.ContainsKey(section)==false)
        //             {
        //                 return string.Empty;
        //             }
        // 
        //             if(_FileMap[section].ContainsKey(key)==false)
        //             {
        //                 return string.Empty;
        //             }
        // 
        //             return _FileMap[section][key];
        //         }

        public List<string> Sections
        {
            get
            {
                List<string> sections = new List<string>();
                foreach (string key in _FileMap.Keys)
                {
                    sections.Add(key);
                }

                return sections;
            }
        }

        public List<string> Keys(string section)
        {
            List<string> keys = new List<string>();
            foreach (string key in _FileMap[section].Keys)
            {
                keys.Add(key);
            }

            return keys;
        }

        public bool ContainsKey(string section, string key)
        {
            return _FileMap[section].ContainsKey(key);
        }

        public bool ContainsKey(string key)
        {
            foreach (string section in _FileMap.Keys)
            {
                if (_FileMap[section].ContainsKey(key))
                {
                    return true;
                }
            }

            return false;
        }

        public bool ContainsSection(string section)
        {
            return _FileMap.ContainsKey(section);
        }
    }
}
